package com.galaxis.wms.security.login;

import com.galaxis.wms.EnumConstant;
import com.galaxis.wms.entity.BasLoginAgent;
import com.galaxis.wms.entity.BasUser;
import com.galaxis.wms.enums.CAuthType;
import com.galaxis.wms.enums.CLoginTarget;
import com.galaxis.wms.security.model.request.NamePasswordLoginReq;
import com.galaxis.wms.security.utils.UserInfoConvertUtils;
import com.yvan.core.mapper.BeanCopyUtils;
import com.yvan.security.SecurityDataSource;
import com.yvan.security.config.SecurityConfig;
import com.yvan.security.exception.UnsupportedLoginTypeException;
import com.yvan.security.login.LoadUser;
import com.yvan.security.model.UserInfo;
import com.yvan.security.model.request.AbstractUserLoginReq;
import org.springframework.core.Ordered;

import javax.servlet.http.HttpServletRequest;

import static com.galaxis.wms.query.QBasLoginAgent.basLoginAgent;
import static com.galaxis.wms.query.QBasUser.basUser;

/**
 * 作者：lizw <br/>
 * 创建时间：2021/12/16 12:51 <br/>
 */
public class DefaultLoadUser implements LoadUser {
    @Override
    public boolean isSupported(SecurityConfig securityConfig, HttpServletRequest request, AbstractUserLoginReq loginReq) {
        return true;
    }

    @Override
    public UserInfo loadUserInfo(SecurityConfig securityConfig, HttpServletRequest request, AbstractUserLoginReq loginReq) {
        if (loginReq instanceof NamePasswordLoginReq) {
            return loadUser(request, (NamePasswordLoginReq) loginReq);
        } else {
            throw new UnsupportedLoginTypeException("不支持的登录类型");
        }
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }

    protected UserInfo loadUser(HttpServletRequest request, NamePasswordLoginReq namePasswordLoginReq) {
        // 根据“loginName”加载用户信息
        BasLoginAgent loginAgent = SecurityDataSource.getQueryDSL().select(basLoginAgent)
                .from(basLoginAgent)
                .where(basLoginAgent.loginTarget.eq(CLoginTarget.PC))
                .where(basLoginAgent.authType.eq(CAuthType.PASSWORD))
                .where(basLoginAgent.loginName.eq(namePasswordLoginReq.getLoginName()))
                .fetchOne();
        if (loginAgent == null || loginAgent.getUserId() == null) {
            return null;
        }
        BasUser user = SecurityDataSource.getQueryDSL().select(basUser)
                .from(basUser)
                .where(basUser.userId.eq(loginAgent.getUserId()))
                .fetchOne();
        request.setAttribute(EnumConstant.REQUEST_ATTR_LOGIN_AGENT, loginAgent);
        request.setAttribute(EnumConstant.REQUEST_ATTR_USER, user);
        UserInfo userInfo = UserInfoConvertUtils.convertToUserInfo(user);
        userInfo.getExt().putAll(BeanCopyUtils.toMap(loginAgent));
        userInfo.getExt().putAll(BeanCopyUtils.toMap(user));
        userInfo.getExt().remove("userId");
        userInfo.getExt().remove("userName");
        userInfo.getExt().remove("loginPassword");
        userInfo.getExt().remove("createBy");
        userInfo.getExt().remove("createAt");
        userInfo.getExt().remove("updateBy");
        userInfo.getExt().remove("updateAt");
        return userInfo;
    }
}
